package com.ys.otms.controller;

import com.ys.otms.dao.IShipDao;
import com.ys.otms.dto.ShipInfoDto;
import com.ys.otms.dto.ShipOwnerDto;
import com.ys.otms.dto.ShipXyInfoDto;
import com.ys.otms.services.ShipXyServices;
import com.ys.otms.services.WxServices;
import com.ys.otms.utils.*;
import com.ys.otms.utils.wxtemplate.GetWxAccessTokenUtil;
import com.ys.otms.utils.wxtemplate.SendWxTemplateMsgUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class TestController {
    private static Logger logger = LoggerFactory.getLogger(TestController.class);
    @RequestMapping("test")
    @ResponseBody
    public JSONObject test() {
        JSONObject a = new JSONObject();
        a.put("aa",11);
        return a;
    }

    @RequestMapping("testSMS")
    @ResponseBody
    public JSONObject testSMS() {
        JSONObject obj = new JSONObject();
        obj.put("phoneNumbers","17309633039");
        obj.put("templateParam","{\"code\":\"123456\"}");
        obj = SMSUtil.SendEmail(obj);
        return obj;
    }

    @RequestMapping("testAccessToken")
    @ResponseBody
    public JSONObject testAccessToken() {
        JSONObject obj = new JSONObject();
        obj.put("tokn", GetWxAccessTokenUtil.getWxAccessToken());
        return obj;
    }

    @RequestMapping("testSendWxTemplateMsg")
    @ResponseBody
    public JSONObject testSendWxTemplateMsg() {
        JSONObject obj = new JSONObject();
        obj.put("tokn", SendWxTemplateMsgUtil.sendWxTemplateMsg(obj));
        return obj;
    }


    @Autowired
    private WxServices wxServices;

    @RequestMapping("testGetFollows")
    @ResponseBody
    public JSONObject testGetFollows() {
        logger.info("testGetFollows  start");
        JSONObject obj = new JSONObject();
        obj.put("aaa",wxServices.getFollows1());
        return obj;
    }

    @RequestMapping("testBeginEndTime")
    @ResponseBody
    public JSONObject testBeginEndTime() {
        logger.info("testBeginEndTime  start");
        JSONObject obj = new JSONObject();
        Long now30 = DateUtil.offsetDay(new Date(), -30).getTime()/1000;
        obj.put("end",new Date().getTime()/1000);
        obj.put("begin",now30);
        return obj;
    }

    @Autowired
    private IShipDao iShipDao;

    @RequestMapping("testUploadShip")
    @ResponseBody
    public JSONObject testUploadShip() {
        JSONObject obj = new JSONObject();
        try {
            List<ShipInfoDto> shipInfoDtoList = OperExcel4ShipUtil.readExcel("D:/shipInfo.xlsx");
            List<ShipInfoDto> shipInfoDtoList1 = new ArrayList<>();
            List<ShipInfoDto> shipInfoDtoList2 = new ArrayList<>();
            List<ShipInfoDto> shipInfoDtoListo = new ArrayList<>();
            ShipInfoDto shipInfoDto = null;

            List<ShipInfoDto> shipInfoDtos = iShipDao.selectShipInfoByShipName(shipInfoDtoList);
            if (shipInfoDtos.size()!=0){
                for (int j = 0; j < shipInfoDtoList.size(); j++) {
                    ShipInfoDto shipInfoDto1 = shipInfoDtoList.get(j);
                    for (int k = 0; k <shipInfoDtos.size() ; k++) {
                        if(shipInfoDto1.getShipName().equals(shipInfoDtos.get(k).getShipName())) {
                            break;
                        }
                        if (k==shipInfoDtos.size()-1){
                            shipInfoDtoList1.add(shipInfoDto1);
                        }
                    }
                }
                if (shipInfoDtoList1.size()==0){
                    return obj;

                }
                List<ShipOwnerDto> shipOwnerDtoList = iShipDao.selectShipOwnerByPhoneNum(shipInfoDtoList1);
                if (shipOwnerDtoList.size()!=0){
                    for (int i = 0; i < shipInfoDtoList1.size(); i++) {
                        for (int j = 0; j <shipOwnerDtoList.size() ; j++) {
                            if (shipOwnerDtoList.get(j).getPhoneNum().equals(shipInfoDtoList1.get(i).getPhoneNum())) {
                                break;
                            }
                            if(j==shipOwnerDtoList.size()-1){
                                shipInfoDto = new ShipInfoDto();
                                shipInfoDto.setPhoneNum(shipInfoDtoList1.get(i).getPhoneNum());
                                shipInfoDtoListo.add(shipInfoDto);
                            }
                        }
                    }
                    if (shipInfoDtoListo.size()!=0){
                        iShipDao.insertShipOwnerByPhoneNum(shipInfoDtoListo);
                    }
                }else{
                    iShipDao.insertShipOwnerByPhoneNum(shipInfoDtoList1);
                }
                List<ShipOwnerDto> shipOwnerDtoList2 = iShipDao.selectShipOwnerByPhoneNum(shipInfoDtoList1);
                for (int a = 0; a < shipOwnerDtoList2.size(); a++) {
                    for (int b = 0; b < shipInfoDtoList1.size(); b++) {
                        if (shipOwnerDtoList2.get(a).getPhoneNum().equals(shipInfoDtoList1.get(b).getPhoneNum())){
                            shipInfoDto = shipInfoDtoList1.get(b);
                            shipInfoDto.setShipOwnerId(shipOwnerDtoList2.get(a).getId());
                            shipInfoDtoList2.add(shipInfoDto);
                            break;
                        }
                    }
                }
            }else{
                List<ShipOwnerDto> shipOwnerDtoList = iShipDao.selectShipOwnerByPhoneNum(shipInfoDtoList);
                if (shipOwnerDtoList.size()!=0){
                    for (int i = 0; i < shipInfoDtoList.size(); i++) {
                        for (int j = 0; j <shipOwnerDtoList.size() ; j++) {
                            if (shipOwnerDtoList.get(j).getPhoneNum().equals(shipInfoDtoList.get(i).getPhoneNum())) {
                                break;
                            }
                            if(j==shipOwnerDtoList.size()-1){
                                shipInfoDto = new ShipInfoDto();
                                shipInfoDto.setPhoneNum(shipInfoDtoList.get(i).getPhoneNum());
                                shipInfoDtoListo.add(shipInfoDto);
                            }
                        }
                    }
                    if (shipInfoDtoListo.size()!=0){
                        iShipDao.insertShipOwnerByPhoneNum(shipInfoDtoListo);
                    }
                }else{
                    iShipDao.insertShipOwnerByPhoneNum(shipInfoDtoList);
                }
                List<ShipOwnerDto> shipOwnerDtoList2 = iShipDao.selectShipOwnerByPhoneNum(shipInfoDtoList);
                for (int a = 0; a < shipOwnerDtoList2.size(); a++) {
                    for (int b = 0; b < shipInfoDtoList.size(); b++) {
                        if (shipOwnerDtoList2.get(a).getPhoneNum().equals(shipInfoDtoList.get(b).getPhoneNum())){
                            shipInfoDto = shipInfoDtoList.get(b);
                            shipInfoDto.setShipOwnerId(shipOwnerDtoList2.get(a).getId());
                            shipInfoDtoList2.add(shipInfoDto);
                            break;
                        }
                    }
                }
            }
            iShipDao.insertShipInfoQuery(shipInfoDtoList2);
        } catch (Exception e) {
            logger.error("testUploadShip  e :{}",e);
        }

        return obj;
    }

    @RequestMapping("testGetComKey")
    @ResponseBody
    public JSONObject testGetComKey() {
        logger.info("testGetComKey  start");
        JSONObject obj = new JSONObject();
        obj.put("msg",ShipDtAPIUtil.getComKey());
        return obj;
    }

    @RequestMapping("testQueryShip")
    @ResponseBody
    public JSONObject testQueryShip() {
        logger.info("testGetComKey  start");
        JSONObject obj = new JSONObject();
        obj.put("keyword", "qingdao");
        return ShipDtAPIUtil.queryShip(obj);
    }

    @RequestMapping("testGetShips")
    @ResponseBody
    public JSONObject testGetShips() {
        logger.info("testGetComKey  start");
        JSONObject obj = new JSONObject();
        obj.put("shipIds","235057836,413328480,412331110");
        return ShipDtAPIUtil.getShips(obj);
    }

    @RequestMapping("testGetShipHistoryVoyage")
    @ResponseBody
    public JSONObject testGetShipHistoryVoyage() throws Exception {
        logger.info("testGetShipHistoryVoyage  start");
        JSONObject obj = new JSONObject();
        obj.put("shipId","413472680");
        obj.put("begin","2018-10-01 10:10:10");
        obj.put("end","2018-12-01 10:10:10");
        return ShipDtAPIUtil.getShipHistoryVoyage(obj);
    }

    @RequestMapping("testGetShipHistoryTrack")
    @ResponseBody
    public JSONObject testGetShipHistoryTrack() throws Exception {
        logger.info("testGetShipHistoryTrack  start");
        JSONObject obj = new JSONObject();
        obj.put("shipId","413472680");
        obj.put("begin","2018-10-01 10:10:10");
        obj.put("end","2018-12-01 10:10:10");
        return ShipDtAPIUtil.getShipHistoryTrack(obj);
    }

}
